package com.wutuo.roster.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.core.utils.TranslateUtil;
import cn.apestar.util.CommonUtil;
import com.wutuo.roster.api.ParentsAPIService;
import com.wutuo.roster.api.request.ListParentsRequest;
import com.wutuo.roster.api.request.ParentsRequest;
import com.wutuo.roster.api.response.ParentsResponse;
import com.wutuo.roster.provider.dao.ParentsMapper;
import com.wutuo.roster.provider.domain.Parents;
import com.wutuo.roster.provider.enums.EnumList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务关系表 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service public class ParentsService extends BaseService<Parents, Long>
    implements ParentsAPIService {

    private Logger logger = LoggerFactory.getLogger(ParentsService.class);

    @Autowired private ParentsMapper parentsMapper;

    @Override protected GenericMapper<Parents, Long> getMapper() {

        // TODO Auto-generated method stub
        return parentsMapper;
    }

    public void addorUpdate(ParentsRequest record) {

        if (record.getId() == null) {
            super.add(record);
        } else
            super.update(record);
    }

    public void del(Long id) {

        ParentsRequest model = new ParentsRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<ParentsResponse> lisForPage(ListParentsRequest request) {

        List<ParentsResponse> list = parentsMapper.lisForPage(request);
        return list;
    }

    public List<ParentsResponse> list(ParentsRequest request) {

        request.setDelFlag(true);
        List list = parentsMapper.list(request);
        TranslateUtil
            .translatEnumList(list, "userType", "userTypeName", EnumList.getUserEnumList());
        return list;
    }

    /**
     * 根据花名册删除家常关系表
     *
     * @param rosterId
     * @return int
     * @author lvh 2016年2月3日 下午12:22:57</br>
     */
    public void deleteParentsByrosterId(Long rosterId) {

        // TODO Auto-generated method stub
        parentsMapper.deleteParentsByrosterId(rosterId);
    }

    /**
     * 获取家长关系信息
     *
     * @param <T>
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public <T> void getPareantByParentsId(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        StringBuffer idIn = CommonUtil.getLongInt(list, tKey);
        ParentsRequest request = new ParentsRequest();
        request.setIds(idIn.toString());
        List<ParentsResponse> listParents = list(request);
        Map map = CommonUtil.listToObject(listParents, "id");
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }

            String id = o.toString();
            String[] ids = id.split(",");
            List<T> s = new ArrayList();
            for (int j = 0; j < ids.length; j++) {
                if (map.containsKey(ids[j])) {
                    Object p = map.get(ids[j]);
                    if (!s.contains(p))
                        s.add((T) p);
                }
            }
            CommonUtil.setProperty(modelS, skey, s);
        }
    }

    public List<Parents> listParentsByRosterId(Long rosterId) {
        return parentsMapper.listParentsByRosterId(rosterId);
    }
}